//
//  SeeCommentVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/19.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "SeeCommentVC.h"
#import "HCSStarRatingView.h"
#import "SeeCommentModel.h"
#import "XLImageViewer.h"

@interface SeeCommentVC (){
    UIView *_imgParent;
    FlexFrameView *header;
}
//@property (nonatomic,strong) CJView *viewStarServer;
//@property (nonatomic,strong) CJView *viewStarDelivery;

//@property (nonatomic,strong) HCSStarRatingView *viewRateServer;
//@property (nonatomic,strong) HCSStarRatingView *viewRateAbnormal;
@property (nonatomic,strong) CJLabel *labComment;
@property (nonatomic,strong) NSMutableArray *arrNetImgs;
@property (nonatomic,strong) SeeCommentModel *model;
@property (nonatomic,copy) NSDictionary *dicData;
@property (nonatomic,copy) NSString *strCommentType; //0-货主给司机打分，1-司机给货主打分
@property (nonatomic,strong) UISegmentedControl *segment; //分段控制
@property (nonatomic,strong) CJView *viewDelivery; //  运输效率
@property (nonatomic,strong) HCSStarRatingView *viewStarDelivery; //运输效率
//@property (nonatomic,copy) NSString *strSpeed;
// 运输效率
@property (nonatomic,strong) CJView *viewTimeGrade; //
@property (nonatomic,strong) HCSStarRatingView *viewStarTimeGrade;
// safetyGrade 运输安全
@property (nonatomic,strong) CJView *viewSafety; //
@property (nonatomic,strong) HCSStarRatingView *viewStarSafetyGrade; //
//@property (nonatomic,copy) NSString *strSafety;
// 服务质量
@property (nonatomic,strong) CJView *viewStarServer;
@property (nonatomic,strong) HCSStarRatingView *viewRateServer;
//@property (nonatomic,copy) NSString *strServer;
//货物损耗   goodsLoss
@property (nonatomic,strong) CJView *viewGoodsLoss;
@property (nonatomic,strong) HCSStarRatingView *viewStarGoodsLoss;
//@property (nonatomic,copy) NSString *strGoodsLoss;
// 客户满意度   satisfactionGrade
@property (nonatomic,strong) CJView *viewSatisfaction;
@property (nonatomic,strong) HCSStarRatingView *viewRateSatisfaction;
//@property (nonatomic,copy) NSString *strSatisfaction;
#pragma mark ----------司机评价货主
// 货物相符性 truthGrade
@property (nonatomic,strong) CJView *viewTruthGrade;
@property (nonatomic,strong) HCSStarRatingView *viewRateTruthGrade;
//付款时效性  timeGrade
@property (nonatomic,strong) CJView *viewPayTimeGrade;
@property (nonatomic,strong) HCSStarRatingView *viewRatePayTimeGrade;
//地址准确性 addressGrade
@property (nonatomic,strong) CJView *viewAddressGrade;
@property (nonatomic,strong) HCSStarRatingView *viewRateAddressGrade;
//装卸时效性 loadGrade
@property (nonatomic,strong) CJView *viewLoadAndUnloadGrade;
@property (nonatomic,strong) HCSStarRatingView *viewRateLoadAndUnloadGrade;
@end

@implementation SeeCommentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setBackLeftButton:@""];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.strCommentType = @"0";
    [self selectSendGoodsType];
    [self loadDataDetail];
}

- (void)selectSendGoodsType{
    NSArray *array = [NSArray arrayWithObjects:@"我评价的",@"评价我的",nil];
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    self.segment.frame = CGRectMake((SCREEN_WIDTH-LW(180))/2, LW(7) + kStatusBarHeight, LW(180), LW(28));
    [self.segment addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventValueChanged];
    [self.image addSubview:self.segment];
    //
    if (DeviceSystemVersion.doubleValue > 13) {
        UIColor *tintColor = [UIColor cjMainColor];
        UIImage *tintColorImage = [UIImage imageWithColor:[UIColor cjMainColor]];
        // Must set the background image for normal to something (even clear) else the rest won't work
        [self.segment setBackgroundImage:[UIImage imageWithColor:self.segment.backgroundColor ? self.segment.backgroundColor : [UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self.segment setBackgroundImage:tintColorImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        [self.segment setBackgroundImage:[UIImage imageWithColor:[tintColor colorWithAlphaComponent:0.2]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
        [self.segment setBackgroundImage:tintColorImage forState:UIControlStateSelected|UIControlStateSelected barMetrics:UIBarMetricsDefault];
        [self.segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor cjWhiteColor], NSFontAttributeName: [UIFont cjTitleFont14]} forState:UIControlStateSelected];
        [self.segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor cjMainColor], NSFontAttributeName: [UIFont cjTitleFont14]} forState:UIControlStateNormal];
        [self.segment setDividerImage:tintColorImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        self.segment.layer.borderWidth = 0.5;
        self.segment.layer.borderColor = [tintColor CGColor];
        self.segment.selectedSegmentIndex = 0;
    }else{
        self.segment.tintColor = [UIColor cjMainColor];
    }
    self.segment.selectedSegmentIndex = 0;
}

//不同分段
-(void)changeType:(UISegmentedControl *)sender{
    
    if (sender.selectedSegmentIndex == 0) {
        self.strCommentType = @"0";
    }else{
        self.strCommentType = @"1";
    }
    
    [header removeFromSuperview];
    [self loadDataDetail];
}

- (void)loadDataDetail{
    
    self.dicData = [[NSDictionary alloc]init];
    NSDictionary *dicPra = @{
        @"orderDetailCode":self.strCode,
        @"commentType":self.strCommentType
        // @"16f113c77b7a42a5a9b7d231fc9ad39e"  //
    };
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/orderComment/orderCommentByDetailCode",CJBaseUrl);
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
        DLog(@"=====  %@",responseObject);
        ws.dicData = (NSDictionary *)responseObject;
        [ws createView];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

/// 创建tableview 等
- (void)createView{

    CGRect rcFrame = CGRectMake(0, kTopHeight, kScreenWidth, 0);
    // SeeCommentTopDivrerView
    NSString *strType = @"";
    if (self.strCommentType.intValue == 0) {
        strType = @"SeeCommentTopView";
    }else{
        strType = @"SeeCommentTopDivrerView";
    }
//    [header removeAllSubviews];
    header = [[FlexFrameView alloc] initWithFlex:strType Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    
    if (self.strCommentType.intValue == 0) {
        
        [self.viewDelivery addSubview:self.viewStarDelivery];
        self.viewStarDelivery.value =  [[CJControl getThreeUnaryValueStr:self.dicData[@"speedGrade"]] floatValue];
        
        [self.viewStarServer addSubview:self.viewRateServer];
        self.viewRateServer.value = [self.dicData[@"safetyGrade"] floatValue];

        [self.viewSafety addSubview:self.viewStarSafetyGrade];
        self.viewStarSafetyGrade.value =  [self.dicData[@"serviceGrade"] floatValue];
//        货物损耗度
        [self.viewGoodsLoss addSubview:self.viewStarGoodsLoss];
        self.viewStarGoodsLoss.value = [self.dicData[@"lossGrade"] floatValue];
        
        [self.viewSatisfaction addSubview:self.viewRateSatisfaction];
        self.viewRateSatisfaction.value = [self.dicData[@"satisfactionGrade"] floatValue];
    }else{
        
        //        货物相符性
        [self.viewTruthGrade addSubview:self.viewRateTruthGrade];
        self.viewRateTruthGrade.value =  [[CJControl getThreeUnaryValueStr:self.dicData[@"truthGrade"]] floatValue];
    
        //付款时效性  timeGrade
        [self.viewPayTimeGrade addSubview:self.viewRatePayTimeGrade];
        self.viewRatePayTimeGrade.value = [[CJControl getThreeUnaryValueStr:self.dicData[@"timeGrade"]] floatValue];
        
        //地址准确性 addressGrade
        [self.viewAddressGrade addSubview:self.viewRateAddressGrade];
        self.viewRateAddressGrade.value = [self.dicData[@"addressGrade"] floatValue];
        
        //装卸时效性 loadGrade
        [self.viewLoadAndUnloadGrade addSubview:self.viewRateLoadAndUnloadGrade];
        self.viewRateLoadAndUnloadGrade.value = [self.dicData[@"loadGrade"] floatValue];
        // 综合满意度
        [self.viewSatisfaction addSubview:self.viewRateSatisfaction];
        self.viewRateSatisfaction.value = [self.dicData[@"multipleGrade"] floatValue];
    }
    
    if ([CJRegular getIsStrEmpty:self.dicData[@"commentContent"]]) {
        self.labComment.hidden = YES;
    }else{
        self.labComment.hidden = NO;
        self.labComment.text = self.dicData[@"commentContent"];
    }
    
    [self.arrNetImgs removeAllObjects];
    NSArray *list = self.dicData[@"imageUrlList"];
    [self.arrNetImgs addObjectsFromArray:list];
    
    if (list.count > 0) {
        for (int i = 0; i < list.count; i++) {
            
            UIImageView* cell = [[UIImageView alloc]init];
            cell.userInteractionEnabled = YES;
    //        [cell enableFlexLayout:YES];
             UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickToSeeBigCell:)];
            cell.tag = i + 1000;
            [cell enableFlexLayout:YES];
            [cell addGestureRecognizer:tap];
            
            [cell setLayoutAttrStrings:@[
                                         @"width",@"75",
                                         @"height",@"75",
                                         @"margin",@"1%",
                                         @"alignItems",@"center",
                                         @"justifyContent",@"center",
                                         ]];
            [cell setViewAttr:@"bgColor" Value:@"#E4E9ED"];
            [cell setViewAttr:@"borderRadius" Value:@"0"];
            [_imgParent insertSubview:cell atIndex:i];
            [_imgParent markDirty];

            [cell sd_setImageWithURL:[NSURL URLWithString:self.arrNetImgs[i]] placeholderImage:[UIImage imageNamed:@"zcj_error_image"]];
        
        }
    }

    [header layoutIfNeeded];
    [self.view addSubview:header];
}

-(void)clickToSeeBigCell:(UIGestureRecognizer*)sender{
    UIView* cell = sender.view;
    [[XLImageViewer shareInstanse] showNetImages:self.arrNetImgs index:(int)cell.tag - 1000 fromImageContainer:cell];
}

#pragma mark ----------lazy
- (NSMutableArray *)arrNetImgs{
    if (!_arrNetImgs) {
        _arrNetImgs = [[NSMutableArray alloc]init];
    }
    return _arrNetImgs;
}
#pragma mark ----------货主评价司机

- (HCSStarRatingView *)viewRateServer{
    if (!_viewRateServer) {
      _viewRateServer =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewRateServer.tintColor = [UIColor cjStarRateColor];
        _viewRateServer.starBorderColor = [UIColor cjClearColor];
        _viewRateServer.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewRateServer.backgroundColor = [UIColor cjClearColor];
        _viewRateServer.enabled = NO;
        _viewRateServer.allowsHalfStars = YES;
        _viewRateServer.maximumValue = 5;
        _viewRateServer.minimumValue = 0;
    }
    return  _viewRateServer;
}

- (HCSStarRatingView *)viewStarDelivery{
    if (!_viewStarDelivery) {
       _viewStarDelivery =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewStarDelivery.tintColor = [UIColor cjStarRateColor];
        _viewStarDelivery.starBorderColor = [UIColor cjClearColor];
        _viewStarDelivery.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewStarDelivery.backgroundColor = [UIColor cjClearColor];
        _viewStarDelivery.enabled = NO;
        _viewStarDelivery.allowsHalfStars = YES;
        _viewStarDelivery.maximumValue = 5;
        _viewStarDelivery.minimumValue = 0;
    }
    return  _viewStarDelivery;
}

- (HCSStarRatingView *)viewStarTimeGrade{
    if (!_viewStarTimeGrade) {
       _viewStarTimeGrade =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewStarTimeGrade.tintColor = [UIColor cjStarRateColor];
        _viewStarTimeGrade.starBorderColor = [UIColor cjClearColor];
        _viewStarTimeGrade.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewStarTimeGrade.backgroundColor = [UIColor cjClearColor];
        _viewStarTimeGrade.enabled = NO;
        _viewStarTimeGrade.allowsHalfStars = YES;
        _viewStarTimeGrade.maximumValue = 5;
        _viewStarTimeGrade.minimumValue = 0;
    }
    return  _viewStarTimeGrade;
}

- (HCSStarRatingView *)viewStarSafetyGrade{
    if (!_viewStarSafetyGrade) {
       _viewStarSafetyGrade =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewStarSafetyGrade.tintColor = [UIColor cjStarRateColor];
        _viewStarSafetyGrade.starBorderColor = [UIColor cjClearColor];
        _viewStarSafetyGrade.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewStarSafetyGrade.backgroundColor = [UIColor cjClearColor];
        _viewStarSafetyGrade.enabled = NO;
        _viewStarSafetyGrade.allowsHalfStars = YES;
        _viewStarSafetyGrade.maximumValue = 5;
        _viewStarSafetyGrade.minimumValue = 0;
    }
    return  _viewStarSafetyGrade;
}

/// 货物损耗度
- (HCSStarRatingView *)viewStarGoodsLoss{
    if (!_viewStarGoodsLoss) {
        _viewStarGoodsLoss =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewStarGoodsLoss.tintColor = [UIColor cjStarRateColor];
        _viewStarGoodsLoss.starBorderColor = [UIColor cjClearColor];
        _viewStarGoodsLoss.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewStarGoodsLoss.backgroundColor = [UIColor cjClearColor];
        _viewStarGoodsLoss.enabled = NO;
        _viewStarGoodsLoss.allowsHalfStars = YES;
        _viewStarGoodsLoss.maximumValue = 5;
        _viewStarGoodsLoss.minimumValue = 0;
    }
    return  _viewStarGoodsLoss;
}

- (HCSStarRatingView *)viewRateSatisfaction{
    if (!_viewRateSatisfaction) {
      _viewRateSatisfaction =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewRateSatisfaction.tintColor = [UIColor cjStarRateColor];
        _viewRateSatisfaction.starBorderColor = [UIColor cjClearColor];
        _viewRateSatisfaction.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewRateSatisfaction.backgroundColor = [UIColor cjClearColor];
        _viewRateSatisfaction.enabled = NO;
        _viewRateSatisfaction.allowsHalfStars = YES;
        _viewRateSatisfaction.maximumValue = 5;
        _viewRateSatisfaction.minimumValue = 0;
    }
    return  _viewRateSatisfaction;
}

#pragma mark ----------司机评价货主

- (HCSStarRatingView *)viewRateTruthGrade{
    if (!_viewRateTruthGrade) {
        _viewRateTruthGrade =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewRateTruthGrade.tintColor = [UIColor cjStarRateColor];
        _viewRateTruthGrade.starBorderColor = [UIColor cjClearColor];
        _viewRateTruthGrade.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewRateTruthGrade.backgroundColor = [UIColor cjClearColor];
        _viewRateTruthGrade.enabled = NO;
        _viewRateTruthGrade.allowsHalfStars = YES;
        _viewRateTruthGrade.maximumValue = 5;
        _viewRateTruthGrade.minimumValue = 0;
    }
    return  _viewRateTruthGrade;
}
// 付款时效性
- (HCSStarRatingView *)viewRatePayTimeGrade{
    if (!_viewRatePayTimeGrade) {
        _viewRatePayTimeGrade =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewRatePayTimeGrade.tintColor = [UIColor cjStarRateColor];
        _viewRatePayTimeGrade.starBorderColor = [UIColor cjClearColor];
        _viewRatePayTimeGrade.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewRatePayTimeGrade.backgroundColor = [UIColor cjClearColor];
        _viewRatePayTimeGrade.enabled = NO;
        _viewRatePayTimeGrade.allowsHalfStars = YES;
        _viewRatePayTimeGrade.maximumValue = 5;
        _viewRatePayTimeGrade.minimumValue = 0;
    }
    return  _viewRatePayTimeGrade;
}

//地址准确性 addressGrade
- (HCSStarRatingView *)viewRateAddressGrade{
    if (!_viewRateAddressGrade) {
        _viewRateAddressGrade =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewRateAddressGrade.tintColor = [UIColor cjStarRateColor];
        _viewRateAddressGrade.starBorderColor = [UIColor cjClearColor];
        _viewRateAddressGrade.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewRateAddressGrade.backgroundColor = [UIColor cjClearColor];
        _viewRateAddressGrade.enabled = NO;
        _viewRateAddressGrade.allowsHalfStars = YES;
        _viewRateAddressGrade.maximumValue = 5;
        _viewRateAddressGrade.minimumValue = 0;
    }
    return  _viewRateAddressGrade;
}

//装卸时效性 loadGrade
- (HCSStarRatingView *)viewRateLoadAndUnloadGrade{
    if (!_viewRateLoadAndUnloadGrade) {
        _viewRateLoadAndUnloadGrade =   [[HCSStarRatingView alloc]initWithFrame:CGRectMake(0,0,120 ,35)];
        _viewRateLoadAndUnloadGrade.tintColor = [UIColor cjStarRateColor];
        _viewRateLoadAndUnloadGrade.starBorderColor = [UIColor cjClearColor];
        _viewRateLoadAndUnloadGrade.emptyStarColor = [UIColor cjEmptyStarRateColor];
        _viewRateLoadAndUnloadGrade.backgroundColor = [UIColor cjClearColor];
        _viewRateLoadAndUnloadGrade.enabled = NO;
        _viewRateLoadAndUnloadGrade.allowsHalfStars = YES;
        _viewRateLoadAndUnloadGrade.maximumValue = 5;
        _viewRateLoadAndUnloadGrade.minimumValue = 0;
    }
    return  _viewRateLoadAndUnloadGrade;
}

@end
